package com.bing.habit.controller.user;

import com.bing.habit.entity.ClockinRecord;
import com.bing.habit.model.result.Result;
import com.bing.habit.model.result.ResultFactory;
import com.bing.habit.service.ClockInLogService;
import com.bing.habit.service.ClockInRecordService;
import com.bing.habit.service.impl.ClockInRecordServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 用户对打卡记录的操作
 */
@RestController
@RequestMapping("/clockin_record")
public class ClockinRecordController {

    @Autowired
    ClockInRecordService clockinRecordService;
    @Autowired
    ClockInLogService clockinLogService;

    /**
     * 增加/更新 打卡记录
     * @param clockinRecord
     * @return
     */
    @PostMapping
    public Result addOrUpdate(@RequestBody ClockinRecord clockinRecord){
        clockinRecord.setClockinTime(new Date());
        clockinRecordService.addOrUpdate(clockinRecord);
        return ResultFactory.buildSuccessResult("添加打卡记录成功");
    }
    /**
     * 取消打卡 --- 删除打卡记录  hid + today?
     */
    @DeleteMapping("/{habitId}")
    public Result delete(@PathVariable("habitId") int habitId){
        ClockinRecord clockinRecord = clockinRecordService.listTodayIn(habitId);
        //删除打卡日志
        if(clockinLogService.listByToday(habitId)){
           clockinLogService.deleteByHidAndToday(habitId);
        }
        //删除打卡记录
        clockinRecordService.delete(clockinRecord.getId());
        return ResultFactory.buildSuccessResult("删除打卡记录成功");
    }

    /**
     * 本周的打卡记录
     * @param habitId
     * @return
     */
    @GetMapping("/list_week/{habitId}")
    public Result listWeek(@PathVariable("habitId") int habitId){
        //得到本周打卡记录集合
        return ResultFactory.buildSuccessResult(
                clockinRecordService.listWeeks(habitId));
    }

    /**
     * 本月打卡记录
     * @param hid
     * @param date
     * @return
     */
    @PostMapping("/list_month")
    public Result listMonth(@RequestParam("hid")String hid,
                            @RequestParam("date") String date) {

        int habitId = Integer.parseInt(hid);
        return ResultFactory.buildSuccessResult(
                clockinRecordService.listMonths(habitId,date));
    }

}
